<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- //头部插件  插槽 分左右 -->
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>本月: 社保在缴 公积金在缴</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="primary" size="small" @click="importExcel">导入excel</el-button>
          <el-button type="danger" size="small" @click="exportExcel">导出excel</el-button>
          <el-button type="success" size="small" @click="closeDialog">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 主体 -->
      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <!-- 头像 -->
          <el-table-column label="头像">
            <template #default="scope">
              <!-- <img :src="scope.row.staffPhoto" alt="" width="80"> -->
              <el-image :src="scope.row.staffPhoto" class="avatar" @click="clickShowCodeDialog(scope.row.staffPhoto)">
                <!-- 具名插槽新语法  图片加载出错的时候自动显示 -->
                <template #error>
                  <!-- <i class="el-icon-picture-outline" /> -->
                  <img src="@/assets/common/bigUserHeader.png" width="80" @click="clickShowCodeDialog(scope.row.staffPhoto)">
                </template>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="scope">
              <span>{{ scope.row.formOfEmployment===1?'正式员工':'临时员工' }}</span>
            </template></el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />

          <el-table-column label="账户状态" />
          <el-table-column label="操作" fixed="right" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="lock(scope.row.id)">查看</el-button>
              <el-button type="text" size="small" @click="distribute(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEm(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination layout="prev, pager, next" :page-size="page.size" :total="total" @current-change="currents" />
        </div>
      </el-card>
    </div>
    <!-- 弹出层 -->
    <add-employee :show-dialog="showDialog" :depts="depts" @closure="showDialog=false" @updates="getgetEmployee()" />
    <!-- 分享展示, 预览的二维码的弹层 -->
    <el-dialog title="二维码" :visible="showCodeDialog" @close="showCodeDialog = false">
      <!-- 二维码 -->
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>
    <!-- 权限分配弹出层 -->
    <assign-role :show-role-dialog="showRoleDialog" :role-i-d="roleID" @closures="showRoleDialog=false,roleID=null" />
  </div>
</template>

<script>
import { getEmployees, delEmployees } from '@/api/employees'
import { departments } from '@/api/departments'
import AddEmployee from './components/add-employee.vue' // 组件
import { tranListToTreeData } from '@/utils/index'
import { getExportData } from '@/components/vendor/excelData'
import QrCode from 'qrcode'
import AssignRole from './components/assign-role.vue'
// 导入局部组件必须在scrip里面导入,在App里面导入
export default {
  // 组件名称
  name: 'Employees',
  // 接收局部注册的组件
  components: {
    AddEmployee,
    AssignRole
  },
  // 组件参数接收来自父组件的数据
  props: {},
  // 组件数据
  data () {
    return {
      total: null, // 总条数
      showCodeDialog: false, // 图片弹出层
      showRoleDialog: false,
      roleID: null,
      page: {
        // 放置页码及相关数据
        page: 1,
        size: 5
      },
      list: [],
      showDialog: false,
      depts: []
    }
  },
  mounted () {
    // 获取数据
    this.getgetEmployee()
  },
  methods: {
    // 获取数据
    async  getgetEmployee () {
      const res = await getEmployees(this.page)
      this.list = res.data.rows
      // 总条数
      this.total = res.data.total
    },
    // 控制弹出层 获取部门数据
    async closeDialog () {
      this.showDialog = !this.showDialog
      const res = await departments()
      // const depts = res.data.depts.map(item => item.introduce && item.id && item.pid)
      // console.log(depts)
      this.depts = tranListToTreeData(res.data.depts)
      // console.log(this.depts)
    },
    // 查看
    lock (id) {
      console.log(id)
      this.$router.push({
        path: '/employees/detail',
        query: {
          id
        }
      })
    },
    // 删除
    delEm (id) {
      // console.log(1)
      this.$confirm('确定删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 删除接口
        await delEmployees(id)
        // console.log(this.list.length)
        // console.log(this.page.page)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        if (this.list.length === 1) {
          this.page.page = this.page.page - 1
        }
        // 获取数据
        this.getgetEmployee()
      }).catch(() => {

      })
    },
    // 分页
    currents (page) {
      this.page.page = page
      // 获取数据
      this.getgetEmployee()
      // console.log(page)
    },
    // 去导入页面
    importExcel () {
      this.$router.push('/upload')
    },
    // 导出
    exportExcel () {
      import('@/components/vendor/Export2Excel').then(async excel => {
        // 1.准备表头中的中英文对应关系
        const headerRelation = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '转正日期': 'correctionTime',
          '部门': 'departmentName'
        }
        // 2.调用接口准备表格数据 (从数组库拉取当前页的最新的数据完成导出)
        const res = await getEmployees(this.page)
        // 3.调用处理方法进行数据处理
        const { data, header } = getExportData(res.data.rows, headerRelation)
        excel.export_json_to_excel({
          header, // 表头数据
          data, // 表格数据 (二维数组)
          filename: '员工列表',
          autoWidth: true,
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 图片转换为二维码
    clickShowCodeDialog (photo) {
      // console.log(photo)
      if (!photo) return // 有图片才显示弹层
      this.showCodeDialog = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.myCanvas, photo)
      })
    },
    // 分配权限
    distribute (id) {
      this.roleID = id
      // console.log(id)
      this.showRoleDialog = true
    }
  }
}
</script>
<style lang='less' scoped>

</style>
